/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cardgame.game;

import java.io.IOException;
import cardgame.network.*;

/**
 *
 * @author Carl
 */
public class Player implements BitSerializable{

    public Player(){
        
    }
    private Game game;
    private int lifepoints = 20;
    private Deck deck = new Deck();
    private Hand hand = new Hand();
    private ManaPool manaPool = new ManaPool();
    private Cards monsters = new Cards(CardPosition.Zone.MONSTER);
    private Graveyard graveyard = new Graveyard();
    private boolean hasAddedManaThisTurn;
    
    public int getIndex(){
        return game.getIndex(this);
    }

    public void setLifepoints(int lifepoints){
        this.lifepoints = lifepoints;
    }

    public int getLifepoints(){
        return lifepoints;
    }
    
    public boolean canAddMana(){
        return (!hasAddedManaThisTurn);
    }
    
    public boolean canCast(Spell spell){
        return canPay(spell.getCost(), spell.getCaster());
    }
    
    public boolean canPay(Cost cost, Card card){
        return (hasEnoughMana(cost.getMana()) && (lifepoints > cost.getLifepoints()) && (!(cost.isTap() && card.isTapped())));
    }
    
    public boolean hasEnoughMana(ManaAmount manaAmount){
        return manaPool.isAvailable(manaAmount);
    }

    public void setGame(Game game){
        this.game = game;
        deck.setOwner(this);
        hand.setOwner(this);
        manaPool.setOwner(this);
        monsters.setOwner(this);
        graveyard.setOwner(this);
    }

    public Game getGame(){
        return game;
    }
    
    public Player getEnemy(){
        return game.getEnemy(this);
    }

    public Deck getDeck(){
        return deck;
    }

    public Hand getHand(){
        return hand;
    }

    public Cards getMonsters(){
        return monsters;
    }

    public ManaPool getManaPool(){
        return manaPool;
    }

    public Graveyard getGraveyard(){
        return graveyard;
    }

    public void setHasAddedManaThisTurn(boolean hasAddedManaThisTurn){
        this.hasAddedManaThisTurn = hasAddedManaThisTurn;
    }

    public boolean hasAddedManaThisTurn(){
        return hasAddedManaThisTurn;
    }

    @Override
    public void write(BitOutputStream outputStream){
        outputStream.writeBits(lifepoints, 8);
        deck.write(outputStream);
        hand.write(outputStream);
        manaPool.write(outputStream);
        monsters.write(outputStream);
        graveyard.write(outputStream);
        outputStream.writeBoolean(hasAddedManaThisTurn);
    }

    @Override
    public void read(BitInputStream inputStream) throws IOException{
        lifepoints = inputStream.readBits(8);
        deck.read(inputStream);
        hand.read(inputStream);
        manaPool.read(inputStream);
        monsters.read(inputStream);
        graveyard.read(inputStream);
        hasAddedManaThisTurn = inputStream.readBoolean();
    }
}
